package com.yuteng.video.controller;

import com.github.pagehelper.PageInfo;
import com.yuteng.commons.response.ResponseData;
import com.yuteng.commons.response.SuccessResponseData;
import com.yuteng.video.config.MinioUtils;
import com.yuteng.video.entity.Result;
import com.yuteng.video.entity.Video;
import com.yuteng.video.service.VideoService;
import com.yuteng.video.service.impl.VideoServiceImpl;
import io.minio.MinioClient;
import io.minio.errors.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;


/**
 * 视频网站服务控制器
 *
 * @author 余腾
 * @description 视频的一些信息
 * @since 2021-10-27 15:41:54
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/video")
public class VideoController {
    private static String url = "http://10.1.42.93:9000";  //minio服务的IP端口
    private static String accessKey = "admin";
    private static String secretKey = "admin123";
    @Autowired
    private final VideoService videoService;//, @RequestParam("picFile") MultipartFile picFile

    @Autowired
    VideoServiceImpl videoServices;
    @PostMapping("/upload")
    public Result upload(@RequestParam("videoFile") MultipartFile multipartFile) {
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            String contentType = multipartFile.getContentType();
            MinioUtils.putObject("file",multipartFile,originalFilename,contentType);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        }
     //   System.out.println(video);
      //   videoServices.add(video);
        return new Result(true,200,"上传成功",null);
    }

    @GetMapping("/downloadVideo")
    public Result downloadVideo(String  fileName, HttpServletResponse response) {
        try {
            //就一秒@!!!!!!!!!!!!!!!!!!!!!!!!!
            String url = MinioUtils.getPresignedObjectUrl("file", fileName, 1);
            InputStream file = MinioUtils.getObject("file", fileName);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/force-download");
            response.setCharacterEncoding("UTF-8");
            IOUtils.copy(file,response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InvalidExpiresRangeException e) {
            e.printStackTrace();
        }
        List list=new ArrayList();
        list.add(url);
        return new Result(true,200,"下载成功",list);
    }
    @GetMapping("/getUrl")
    public Result getUrl(String fileName, HttpServletResponse response){
        try {
            //就一秒@!!!!!!!!!!!!!!!!!!!!!!!!!
            String url = MinioUtils.getPresignedObjectUrl("file", fileName, 1);
            InputStream file = MinioUtils.getObject("file", fileName);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/force-download");
            response.setCharacterEncoding("UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InvalidExpiresRangeException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        }
        List list=new ArrayList();
        list.add(url);
        return new Result(true,200,"ok",list);
    }
    @GetMapping
    public Result getAllVideo(@RequestParam(defaultValue = "1") int pageNum,
                              @RequestParam(defaultValue = "5") int pageSize){
        PageInfo all = videoServices.getAll(pageNum, pageSize);
        List list=new ArrayList();
        list.add(all);
        return new Result(true,200,"ok",list);
    }
    @PutMapping
    public Result updateVideo(@RequestBody Video video){
        videoServices.updateVideo(video);
        return new Result(true,200,"ok",null);
    }
    @DeleteMapping
    public Result deleteVideoById(Long id){
        videoServices.deleteVideoById(id);
        return new Result(true,200,"ok",null);
    }
    @PostMapping
    public Result add(@RequestBody Video video){
        videoServices.add(video);
        return new Result(true,200,"ok",null);
    }
}